package com.lt.bookkeeping.recycleView;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ResourceCursorAdapter;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.lt.bookkeeping.R;
import com.lt.bookkeeping.dataSave.BookKeepData;

import java.util.ArrayList;
import java.util.List;

/**
 * @author LiuTong
 */
public class BookKeepRecycleViewAdapter extends RecyclerView.Adapter<BaseHolder> {

    private final int FOOT_VIEW_TYPE = 1;
    private final int ITEM_VIEW_TYPE = 2;
    private final int NO_DATA_TYPE = 3;
    private int status = FootViewHolder.LOADING;
    private List<BookKeepData> bookKeepDataList = new ArrayList<>();

    public BookKeepRecycleViewAdapter(List<BookKeepData> bookKeepDataList) {
        this.bookKeepDataList = bookKeepDataList;
    }

    public void upDate(List<BookKeepData> upDataBookKeepDataList){
        if(upDataBookKeepDataList != null && upDataBookKeepDataList.size() > 0){
            int startCounts = bookKeepDataList.size();
            bookKeepDataList.addAll(upDataBookKeepDataList);
            notifyItemRangeInserted(startCounts,upDataBookKeepDataList.size());
            notifyItemRangeChanged(startCounts,upDataBookKeepDataList.size());
        }
    }

    @NonNull
    @Override
    public BaseHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = null;
        if(viewType == ITEM_VIEW_TYPE){
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_data_view,parent,false);
            return new ItemViewHolder(view);
        }else if(viewType == FOOT_VIEW_TYPE){
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_foot_view,parent,false);
            return new FootViewHolder(view);
        }else {
            view = LayoutInflater.from(parent.getContext()) .inflate(R.layout.item_no_data_viewm,parent,false);
             return new NoDataHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull BaseHolder holder, int position) {
        if(holder instanceof ItemViewHolder){
            ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
            itemViewHolder.onBind(bookKeepDataList.get(position));

        }else if(holder instanceof FootViewHolder){
            FootViewHolder footViewHolder = (FootViewHolder) holder;
            footViewHolder.onBind(status);

        }else if(holder instanceof NoDataHolder){

        }
    }

    @SuppressLint("NotifyDataSetChanged")
    public void setFootStatus(int status){
        this.status = status;
        notifyDataSetChanged();
    }



    @Override
    public int getItemCount() {
        return bookKeepDataList.size()+1;
    }

    @Override
    public int getItemViewType(int position) {
        /***
         * 最后一个view为FOOT_VIEW
         */
        if(getItemCount() == 1){
            return NO_DATA_TYPE;
        } else if(position + 1 == getItemCount()){
            return FOOT_VIEW_TYPE;
        }else {
            return ITEM_VIEW_TYPE;
        }
    }
}
